/**
 * @author zcz
 * @class Ext.ux.tips
 * 版本
 * @version 1
 */
Ext.ux.tips = Ext.extend(Ext.util.Observable, {
    dh: Ext.DomHelper,
    easing: 'easeOut',
    task: null,
    left: 0,
    top: 0,
    autoDestroy: false,
    constructor: function(config){
        this.config = config || {};
        Ext.apply(this, config);
        
        Ext.ux.tips.superclass.constructor.call(this, config);
        this.initializes();
    },
    initializes: function(){
        this.task = new Ext.util.DelayedTask(function(){
            if (this.tips.isVisible()) 
                this.destroy();
        }, this);
        this.tipsHelper = this.creatTips();
        if (typeof this.trigger == 'string') {
            this.trigger = Ext.get(this.trigger);
        }
        if (this.event) 
            this.trigger.on(this.triggerEvent, this.renderTo, this);
        else 
            this.renderTo();
    },
    renderTo: function(){
        var bd = Ext.getBody();
        var position = this.trigger.getXY();
        if (this.left > 0) {
            position[0] = position[0] + this.left + this.trigger.getWidth() + 5;
        }
        else {
            position[0] = position[0] + this.trigger.getWidth() + 5;
        }
        if (this.top > 0) {
            position[1] = position[1] + this.top;
        }
        var t = this.tipsHelper;
        if (this.tips) {
            this.destroy();
        }
        this.tips = this.dh.append(bd, t, true);
        this.tips.setXY(position);
        var _tipsMessageHelper = this.messageContent();
        var _tips = this.tips, _errorImgHelper;
        var tipsMessageHelper = this.dh.append(_tips, _tipsMessageHelper, true);
        if (this.traffic == 'error') {
            _errorImgHelper = this.tipsErrorImage();
            this.tips.addClass('Ext-ux-tips-error-border');
            this.dh.insertFirst(_tips, _errorImgHelper);
            Ext.fly(this.megId).addClass('Ext-ux-tips-error-msg');
        }
        this.tips.show(this.easing);
        if (this.autoDestroy) 
            this.task.delay(5000);
    },
    creatTips: function(){
        return {
            tag: "div",
			trigger:this.trigger.id,
            id: this.id ? this.id : Ext.id(),
            cls: (this.cls ? "Ext-ux-tips " + this.cls : "Ext-ux-tips")
        };
    },
    messageContent: function(){
        this.megId = this.tipsMessage ? (this.tipsMessage.id ? this.tipsMessage.id : Ext.id()) : Ext.id();
        return this.tipsMessage ? this.tipsMessage : {
            tag: "span",
            id: this.megId,
            cls: (this.msgCls ? ' ' + this.msgCls : ''),
            html: this.message || ''
        };
    },
    tipsErrorImage: function(){
        return {
            tag: 'img',
            align: 'absmiddle',
            height: '16',
            width: '16',
            src: '/resource/images/s.gif',
            cls: 'Ext-ux-tips-error'
        };
    },
    closeTips: function(){
        this.tips.hide(this.easing);
    },
    destroy: function(){
        this.tips.remove();
    }
});
